package com.example.work4.http

import android.content.Context
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.Toast
import com.example.work4.common.FIN
import com.example.work4.http.imp.ImageRetrofit
import com.example.work4.po.ResultBean
import com.example.work4.po.SSOInfo
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.File

class ImageRetrofitImp {

    var handler: Handler?=null

    var context: Context?=null

    private var retrofit = BaseRetrofit.get(FIN.BaseUrl)

    private var imageUpload = retrofit.create(ImageRetrofit::class.java)

    fun getUploadToken(filePath:String)
    {
        SSOInfo.token?.let { imageUpload.getUploadToken(it) }?.enqueue(object :
            Callback<ResultBean<Map<String,String>>>
        {
            override fun onResponse(
                call: Call<ResultBean<Map<String, String>>>,
                response: Response<ResultBean<Map<String, String>>>
            ) { val code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    val map: Map<String, String>? = response.body()?.data
                    val qiniu = QiniuUpload();
                    qiniu.handler = handler
                    val array = filePath.split(".")
                    val token = map?.get("token")
                    val uploadName = map?.get("key") + "." + array[array.lastIndex]
                    qiniu.uploadImg(filePath,uploadName,token)
                } else {
                    Toast.makeText(context, "获取七牛云TOKEN失败！", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<Map<String, String>>>, t: Throwable) {

            }
        })
    }

    fun imageUpload(file:File)
    {
        val fileRQ:RequestBody = RequestBody.create(MediaType.parse("image/*"), file)
        val body =MultipartBody.Part.createFormData("image", file.name, fileRQ);
        SSOInfo.token?.let { imageUpload.uploadOneFile(it, body ) }?.enqueue(object :
            Callback<ResultBean<Map<String,String>>>
        {
            override fun onResponse(
                call: Call<ResultBean<Map<String, String>>>,
                response: Response<ResultBean<Map<String, String>>>
            ) { val code = response.body()?.code
                response.body()?.msg?.let { it2 -> Log.i("e", it2) }
                if (code == 0) {
                    val map: Map<String, String>? = response.body()?.data
                    val message=Message()
                    message.what=FIN.MESSIMAGEUPLOAD
                    message.obj=map?.get("uri")
                    handler?.sendMessage(message)
                } else {
                    Toast.makeText(context, "上传失败稍后重试！", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ResultBean<Map<String, String>>>, t: Throwable) {
                System.out.println(t.toString())
            }
        })
    }
}